package com.example.lala.mvp.view;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.lala.R;
import com.example.mylibrary.mvp.view.BaseActivity;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.HashMap;

public class RegisterActivity  extends BaseActivity {

    private android.widget.EditText et1;
    private android.widget.EditText et2;
    private android.widget.EditText et3;
    private android.widget.Button bt;

    @Override
    public int bondLayout() {
        return R.layout.activity_register;
    }

    @Override
    public void initView() {
        et1 = (EditText) findViewById(R.id.et1);
        et2 = (EditText) findViewById(R.id.et2);
        et3 = (EditText) findViewById(R.id.et3);
        bt = (Button) findViewById(R.id.bt);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String trim = et1.getText().toString().trim();
                final String trim1 = et2.getText().toString().trim();
//                非空判断
                if(trim.isEmpty()){
                    Toast.makeText(RegisterActivity.this, "用户名不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if(trim1.isEmpty()){
                    Toast.makeText(RegisterActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }

                String trim2 = et3.getText().toString().trim();
                HashMap<String ,String> map = new HashMap<>();
                map.put("username",trim);
                map.put("password",trim1);
                map.put("repassword",trim2);
                OkGo.<String>post("https://www.wanandroid.com/user/register")
                        .params(map)
                        .execute(new StringCallback() {
                            @Override
                            public void onSuccess(Response<String> response) {
                                String body = response.body();
                                Toast.makeText(RegisterActivity.this, ""+body, Toast.LENGTH_SHORT).show();;
                                Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
                                intent.putExtra("name",trim);
                                intent.putExtra("pwd",trim1);
                                startActivity(intent);
                            }
                        });

            }
        });
    }

    @Override
    public void initData() {

    }

    @Override
    public void Toast(String s) {

    }
}
